<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/


session_start ();

define ('EXPLAY', 1);

include $_SERVER['DOCUMENT_ROOT'].'/engine/settings.php';
include $_SERVER['DOCUMENT_ROOT'].'/engine/mysql.class.php';
define ('MySQL_SERVER', $server);
define ('MySQL_USER', $dbusername);
define ('MySQL_PASSWORD', $dbpassword);
define ('MySQL_DB_NAME', $dbname);
define ('MySQL_MODE', 'html');
$db = MySQL::getInstance ();
$db->connect();


if ($_GET['mod'] == "bot") {
	mt_srand (microtime() * time() * mt_rand(0,999));
	(isset($_GET['comment']))? $alfavit = "0123456789" : $alfavit = "23456789qwertyupasdfgkzxcvbm";
	$a = mt_rand(0, strlen($alfavit));
	$b = mt_rand(0, strlen($alfavit));
	$c = mt_rand(0, strlen($alfavit));
	$d = mt_rand(0, strlen($alfavit));
	$e = mt_rand(0, strlen($alfavit));
	$_SESSION['secret_code'] = "";
	$_SESSION['secret_code'] .= $alfavit[$a];
	$_SESSION['secret_code'] .= $alfavit[$b];
	$_SESSION['secret_code'] .= $alfavit[$c];
	$_SESSION['secret_code'] .= $alfavit[$d];
	$_SESSION['secret_code'] .= $alfavit[$e];
	//$_SESSION['secret_code'] = mt_rand (0, 9).mt_rand (0, 9).mt_rand (0, 9).mt_rand (0, 9).mt_rand (0, 9);
	header('Content-type: image/gif');
	//$image = ImageCreateFromGIF("images/bot.gif");
	
	$image = imagecreate (100, 40);
	imagecolorallocate ($image, 255, 255, 255);
	
	// Noise
	$_color = mt_rand (170,230);
	$color = imagecolorallocate ($image, $_color, $_color, $_color);
	for ($i=0; $i<650; $i++) {
		$x = mt_rand (0, imagesx($image));
		$y = mt_rand (0, imagesy ($image));
		imagesetpixel ($image, $x, $y, $color);
	}
	
	// Line
	imagesetthickness ($image, 4);
	for ($i=0; $i<2; $i++) {
		$y1 = mt_rand (0, imagesy($image));
		$y2 = mt_rand (0, imagesy($image));
		$y3 = mt_rand (0, imagesy($image));
		$y4 = mt_rand (0, imagesy($image));
		$x1 = mt_rand (10, imagesx($image)-70);
		$x2 = mt_rand (90, imagesx($image)-10);
		imageline ($image, 0, $y1, $x1, $y2, $color);
		imageline ($image, $x1, $y2, $x2, $y3, $color);
		imageline ($image, $x2, $y3, imagesx($image), $y4, $color);
	}
	
	
	// Print Code
	$x = mt_rand (0,7);
	for ($i=0; $i<5; $i++) {
		$color = imagecolorallocate ($image, mt_rand (70,170), mt_rand (70,170), mt_rand (70,170));
		$y = mt_rand (23,35);
		$r = mt_rand (0,20)-mt_rand (0,30);
		imagettftext ($image, 19, $r, $x, $y, $color, 'images/font.ttf', $_SESSION['secret_code'][$i]);
		$x = $x + 17;
	}
	imagegif ($image);
	imagedestroy ($image);
}
elseif ($_GET['mod'] == "avatar" && isset($_GET['id'])) {
	
	$user = intval($_GET['id']);
	$avatar = $_SERVER['DOCUMENT_ROOT'].'/images/avatars/'.$user.'.jpg';
	$small = $_SERVER['DOCUMENT_ROOT'].'/images/avatars/'.$user.'_small.jpg';

	if (!file_exists($avatar)) {
		$f = fopen ($_SERVER['DOCUMENT_ROOT'].'/images/avatars/noavatar_small.jpg', 'rb');
		header("Content-Type: image/jpeg");
		header("Content-Length: ".filesize($_SERVER['DOCUMENT_ROOT'].'/images/avatars/noavatar_small.jpg'));
		fpassthru($f);
		exit;
	}
	
	if (!file_exists ($small)) {
	
		include $_SERVER['DOCUMENT_ROOT'].'/engine/image.class.php';
		
		try {
			$image = new imageResize (
				$avatar,
				$user.'_small',
				'images/avatars',
				array(24, 24),
				false,
				80
			);
			$image->resize (false);
		}
		catch (Exception $e) {
			exit;
		}
		
	}
	
	$f = fopen ($small, 'rb');
	header("Content-Type: image/jpeg");
	header("Content-Length: ".filesize($small));
	fpassthru($f);
	exit;
	
	
}
elseif ($_GET['mod'] == "day_stat") {
	header("Content-type: image/jpeg");
	$image = imagecreate (560, 300);
	//imagecolorallocate ($image, 255, 255, 255);
	
	$white = imagecolorallocate ($image, 255, 255, 255);
	$black = imagecolorallocate ($image, 0, 0, 0);
	$silver = imagecolorallocate ($image, 220, 220, 220);
	$red = imagecolorallocate ($image, 215, 30, 30);
	
	// Make grid
	imageline ($image, 520, 40, 520, 290, $black);
	imageline ($image, 10, 40, 520, 40, $black);
	imageline ($image, 519, 80, 10, 80, $silver);
	imageline ($image, 519, 120, 10, 120, $silver);
	imageline ($image, 519, 160, 10, 160, $silver);
	imageline ($image, 519, 200, 10, 200, $silver);
	imageline ($image, 519, 240, 10, 240, $silver);
	imageline ($image, 519, 280, 10, 280, $silver);
	$x = 16;
	for ($i=24; $i>=0; $i-=2) {
		imagettftext ($image, 10, 205, $x+12, 10, $black, "images/stat.ttf", $i.":00");
		imageline ($image, $x, 40, $x, 45, $black);
		$x += 42;
	}
	
	// Built statistics
	// Get values and consider max
	$count = array ();
	$today = time() - ( intval(date(H))*3600 + intval(date(i)*60) );
	$hour = $today + 3600;
	for ($i=0; $i<24; $i++) {
		$get = $db->num_rows ($db->query ("SELECT * FROM ".DB_PEREFIX."_statistics WHERE date > $today AND date < $hour"));
		array_push ($count, $get);
		$today += 3600;
		$hour += 3600;
	}
	$maxim = 0;
	for ($i=0; $i<count($count); $i++) {
		$maxim_ = true;
		for ($z=0; $z<count($count); $z++)
			if ($count[$i] < $count[$z]) $maxim_ = false;
		if ($maxim_ == true) $maxim = $count[$i];
	}
	while ($maxim % 10 != 0) $maxim++;
	imagettftext ($image, 10, 180, 550, 280, $black, "images/stat.ttf", $maxim);
	imagettftext ($image, 10, 180, 550, 160, $black, "images/stat.ttf", $maxim/2);
	//$image = imagerotate ($image, 180, $white);
	
	// Built graphic
	imagesetthickness ($image, 3);
	$x = 520;
	for ($i=0; $i<24; $i++) {
		$abs = $count[$i]/$maxim*240;
		$abs = sprintf("%.0f", $abs);
		$abs_next = $count[$i+1]/$maxim*240;
		$abs_next = sprintf("%.0f", $abs_next);
		imageline ($image, $x, $abs+40, $x-21, $abs_next+40, $red);
		$x -= 21;
	}	
	$image = imagerotate ($image, 180, $white);
	imagejpeg ($image, null, 90);
	imagedestroy ($image);
}

elseif ($_GET['mod'] == "month_stat") {	
	header("Content-type: image/jpeg");
	$image = imagecreate (560, 300);
	//imagecolorallocate ($image, 255, 255, 255);
	
	$white = imagecolorallocate ($image, 255, 255, 255);
	$black = imagecolorallocate ($image, 0, 0, 0);
	$silver = imagecolorallocate ($image, 220, 220, 220);
	$red = imagecolorallocate ($image, 215, 30, 30);
	
	// Make grid
	imageline ($image, 520, 40, 520, 290, $black);
	imageline ($image, 10, 40, 520, 40, $black);
	imageline ($image, 519, 80, 10, 80, $silver);
	imageline ($image, 519, 120, 10, 120, $silver);
	imageline ($image, 519, 160, 10, 160, $silver);
	imageline ($image, 519, 200, 10, 200, $silver);
	imageline ($image, 519, 240, 10, 240, $silver);
	imageline ($image, 519, 280, 10, 280, $silver);
	$x = 10;
	for ($i=31; $i>=1; $i--) {
		imageline ($image, $x, 40, $x, 45, $black);
		$x += 17;
	}
	$be_today = time() - ( intval(date(H))*3600 + intval(date(i)*60) ) + 1;
	$x = 58;
	for ($i=1; $i<=15; $i++) {
		//if (date("l", $be_today) == "Sunday" || date("l", $be_today) == "Saturday") $color = $red;
		/*else*/ $color = $black;
		imagettftext ($image, 8, 205, $x, 13, $color, "images/stat.ttf", date("D d.m", $be_today));
		$be_today -= 2*86400;
		$x += 34;
	}
	
	// Built statistics
	// Get values and consider max
	$count = array ();
	$today = time() - ( intval(date(H))*3600 + intval(date(i)*60) );
	$tomorrow = $today + 86400;
	for ($i=0; $i<31; $i++) {
		$get = $db->num_rows ($db->query ("SELECT * FROM ".DB_PEREFIX."_statistics WHERE date > $today AND date < $tomorrow"));
		array_push ($count, $get);
		$today -= 86400;
		$tomorrow -= 86400;
	}
	$maxim = 0;
	for ($i=0; $i<count($count); $i++) {
		$maxim_ = true;
		for ($z=0; $z<count($count); $z++)
			if ($count[$i] < $count[$z]) $maxim_ = false;
		if ($maxim_ == true) $maxim = $count[$i];
	}
	while ($maxim % 10 != 0) $maxim++;
	imagettftext ($image, 10, 180, 550, 280, $black, "images/stat.ttf", $maxim);
	imagettftext ($image, 10, 180, 550, 160, $black, "images/stat.ttf", $maxim/2);
	//$image = imagerotate ($image, 180, $white);
	
	// Built graphic
	imagesetthickness ($image, 3);
	$x = 520;
	for ($i=29; $i>=0; $i--) {
		$abs = $count[$i]/$maxim*240;
		$abs = sprintf("%.0f", $abs);
		$abs_next = $count[$i-1]/$maxim*240;
		$abs_next = sprintf("%.0f", $abs_next);
		//imageline ($image, $x, $abs+40, $x-17, $abs_next+40, $red);
		imagefilledrectangle ($image, $x-1, $abs+40, $x-10, 41, $red);
		$x -= 17;
	}
	$image = imagerotate ($image, 180, $white);
	imagejpeg ($image, null, 90);
	imagedestroy ($image);
}


function thumb_avatar ($src) {
		
	try {
		$image = new imageResize (
			$src,
			$id.'',
			'images/avatars',
			array(24, 24),
			false,
			80
		);
		$image->resize ();
	}
	catch (Exception $e) {
		exit;
	}

}